package middle.id_1_100;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

public class hua19_pass {
    // 待实现函数，在此函数中填入答题代码
    private static int[] getActiveUserNum(String[] logs) {
        //创建并初始化
        Map<Integer, Set<String>> dailyActivate=new HashMap<>(); //用于统计日活
        Set<String> month_set=new HashSet<>(); // 用于统计月活
        for(int i=0;i<logs.length;i++){ // 遍历log
            String log=logs[i]; // "2020-02-01|192.168.218.218|/login.do|success"
            String[] parts = log.split("\\|");
            String date=parts[0];
            String ip=parts[1];
            String url=parts[2];
            String result=parts[3];
            if(!url.equals("/login.do") || !result.equals("success")){
                continue; //不满足活跃用户条件，直接下次循环
            }
            //标准化处理一下ip
            String cleanip=cleanIp(ip);
            //处理date，获取day
            String[] date_parts=date.split("\\-"); // "2020-02-01"
            int day=Integer.valueOf(date_parts[2]);
            if(!dailyActivate.containsKey(day)){
                dailyActivate.put(day, new HashSet<>());
            }
            dailyActivate.get(day).add(cleanip);
            month_set.add(cleanip);
        }
        //统计一下每个Set的size()，即为当天的日活
        int[] dayuser=new int[32]; // 1-31的索引存储对应日期的日活
        for(int i=1;i<32;i++){
            dayuser[i]=dailyActivate.getOrDefault(i, new HashSet<>()).size();
        }
        dayuser[0]=month_set.size();
        return dayuser;
    }
    private static String cleanIp(String ip) {
        String[] splits = ip.split("\\.");
        return String.valueOf(Integer.parseInt(splits[0])) + "."
                + String.valueOf(Integer.parseInt(splits[1])) + "."
                + String.valueOf(Integer.parseInt(splits[2])) + "."
                + String.valueOf(Integer.parseInt(splits[3]));
    }

}
